package com.briup.cms.service.impl;



import com.briup.cms.bean.Category;
import com.briup.cms.dao.CategoryMapper;
import com.briup.cms.dao.extend.CategoryExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.ICategoryService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 栏目业务实现类
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private CategoryExtendMapper categoryExtendMapper;

    // 分页查询栏目信息
    @Override
    public PageInfo<Category> pageQueryAll(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize,true);

        List<Category> list = categoryExtendMapper.pageQueryAll();

        if(list == null){
            list = new ArrayList<>();
        }

        return new PageInfo<>(list);
    }

    // 通过id删除栏目
    @Override
    @Transactional
    public void deleteById(int id) throws CustomerException {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if(category == null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CATEGORY_NOT_EXIST);
        }

        categoryMapper.deleteByPrimaryKey(id);

    }

    // 批量删除栏目
    @Override
    @Transactional
    public void batchDelete(int[] ids) throws CustomerException {
        for(int id : ids){
            this.deleteById(id);
        }
    }

    // 保存栏目信息
    @Transactional
    @Override
    public void saveCategory(Category category) throws CustomerException {
        categoryMapper.insert(category);
    }

    // 更新栏目信息
    @Transactional
    @Override
    public void updateCategory(Category category) throws CustomerException {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    // 查询所有栏目信息，不分页
    @Override
    public List<Category> findAll() throws CustomerException {
        List<Category> list = categoryExtendMapper.findAll();
        if(list == null){
            list = new ArrayList<>();
        }
        return list;
    }
}